package com.kjc.demo.mybatis.ge.controller;


import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kjc.demo.mybatis.ge.entity.IdcardQueryRecord;
import com.kjc.demo.mybatis.ge.service.impl.IdcardQueryRecordServiceImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kangjichang
 * @since 2019-12-30
 */
@RestController
@RequestMapping("/idcardRecord")
@Slf4j
public class IdcardQueryRecordController {
	
	@Autowired
	IdcardQueryRecordServiceImpl idcardQueryRecordService;
	
	@GetMapping("/idcardRecordQuery")
	public String zqQuery(HttpServletRequest request) throws Exception {
		String name = request.getParameter("name");//姓名
		String idCard = request.getParameter("idCard");//身份证号
		IdcardQueryRecord idcardQueryRecord = new IdcardQueryRecord();
		QueryWrapper<IdcardQueryRecord> wrapper = new QueryWrapper<IdcardQueryRecord>();
        wrapper.eq("name", name);
        wrapper.eq("idcard", idCard);
        wrapper.orderByDesc("gmt_create");
        List<IdcardQueryRecord> vehicleList = idcardQueryRecordService.list(wrapper);
	    ObjectMapper mapper = new ObjectMapper();
		String result = mapper.writeValueAsString(vehicleList);
		return result;
	}
	
	@GetMapping("/idcardRecordAdd")
	public String idcardRecordAdd(HttpServletRequest request) throws Exception {
		String name = request.getParameter("name");//姓名
		String idCard = request.getParameter("idCard");//身份证号
		
		IdcardQueryRecord idcardQueryRecord = new IdcardQueryRecord();
		idcardQueryRecord.setName(name);
		idcardQueryRecord.setIdcard(idCard);
		idcardQueryRecord.setServiceProvider("kjc");
		idcardQueryRecord.setGmtCreate(LocalDateTime.now());
		idcardQueryRecord.setGmtModified(LocalDateTime.now());
		idcardQueryRecord.setAppid("123456897");
		idcardQueryRecord.setRequestId(UUID.randomUUID().toString().replace("-", ""));
		boolean num = false;
		try{
			log.info("【IdcardQueryRecord】插入，{}",  JSON.toJSONString(idcardQueryRecord));
			num = idcardQueryRecordService.save(idcardQueryRecord);
			log.info("【IdcardQueryRecord】插入，{}",  num);
		}catch(Exception e){
			log.error("【IdcardQueryRecord】插入异常", e);
			e.printStackTrace();
		}
		
	    ObjectMapper mapper = new ObjectMapper();
		String result = mapper.writeValueAsString(idcardQueryRecord);
		return result + num;
	}
	
	@GetMapping("/idcardRecordpayQuery")
	public String payquery(HttpServletRequest request) throws Exception {
		String name = request.getParameter("name");//姓名
		String idCard = request.getParameter("idCard");//身份证号
	    Page<IdcardQueryRecord> page = new Page<>(1,5);
		IdcardQueryRecord idcardQueryRecord = new IdcardQueryRecord();
		QueryWrapper<IdcardQueryRecord> wrapper = new QueryWrapper<IdcardQueryRecord>();
        wrapper.eq("name", name);
        wrapper.eq("idcard", idCard);
        wrapper.orderByDesc("gmt_create");
//        Page<IdcardQueryRecord> pageResult = idcardQueryRecordService.page(page, wrapper);
        Page<IdcardQueryRecord> pageResult = idcardQueryRecordService.page(page);
        List<IdcardQueryRecord> list = pageResult.getRecords();
	    ObjectMapper mapper = new ObjectMapper();
		String result = mapper.writeValueAsString(list);
		return result;
	}
}
